package com.chenfan.mcn.mq.producer;

import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.clients.baseinfo.BaseCustomer;
import com.chenfan.mcn.clients.baseinfo.BaseInfoClient;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.dao.AnnualRebateApprovalMapper;
import com.chenfan.mcn.dao.BillAssociationDetailMapper;
import com.chenfan.mcn.dao.DiscountApprovalMapper;
import com.chenfan.mcn.enums.AnnualTypeEnum;
import com.chenfan.mcn.enums.FinanceArapTypeEnum;
import com.chenfan.mcn.enums.FinanceChargeTypeEnum;
import com.chenfan.mcn.enums.FinanceSettTemplateEnum;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.AnnualRebateApproval;
import com.chenfan.mcn.model.DiscountApproval;
import com.chenfan.mcn.mq.AbstractMessageProducer;
import com.chenfan.mcn.mq.MessageProducer;
import com.chenfan.mcn.mq.message.CfChargeCommonMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author liuyang
 * @Date 2021/3/5
 * @Time 15:41
 * @Version V1.0
 **/

@Slf4j
@Service
public class AnnualRebateApprovalMessageProducer extends AbstractMessageProducer implements MessageProducer<Long> {

    @Autowired
    private AnnualRebateApprovalMapper annualRebateApprovalMapper;

    @Autowired
    private DiscountApprovalMapper discountApprovalMapper;

    @Autowired
    private BaseInfoClient baseInfoClient;

    @Autowired
    private BillAssociationDetailMapper billAssociationDetailMapper;


    @Async
    @Override
    public void asyncSendRecordToMq(Long annualRebateApprovalId) {
        log.info("进入客户返点审批生成费用推送MQ方法==》annualRebateApprovalId={}", annualRebateApprovalId);
        AnnualRebateApproval annualRebateApproval = annualRebateApprovalMapper.selectById(annualRebateApprovalId);
        if (null == annualRebateApproval) {
            throw new BusinessException(McnErrorCode.ANNUAL_NOT_EXISTS);
        }
        CfChargeCommonMessage chargeMessage = new CfChargeCommonMessage();
        if (annualRebateApproval.getAnnualType() == AnnualTypeEnum.SINGLE_REBATE.code()) {
            chargeMessage.setChargeType(FinanceChargeTypeEnum.CUSTOMER_REBATE_CHARGE.getType());
            chargeMessage.setFinanceEntity(billAssociationDetailMapper.getFinanceEntity(annualRebateApproval.getId()));
            setBalance(chargeMessage, annualRebateApproval.getCustomerId());
        }
        if (annualRebateApproval.getAnnualType() == AnnualTypeEnum.ANNUAL_REBATE.code()) {
            chargeMessage.setChargeType(FinanceChargeTypeEnum.YEAR_REBATE_CHARGE.getType());
            DiscountApproval discountApproval = discountApprovalMapper.selectByDiscountCode(annualRebateApproval.getDiscountCode());
            chargeMessage.setFinanceEntity(discountApproval.getContractSubject());
            setBalance(chargeMessage, annualRebateApproval.getCustomerId());
        }
        chargeMessage.setArapType(FinanceArapTypeEnum.PAY.getType());
        chargeMessage.setChargeSourceCode(annualRebateApproval.getRebateContractCode());
        chargeMessage.setChargeSourceDetail(annualRebateApproval.getRebateContractCode());
        chargeMessage.setAmountPp(annualRebateApproval.getConfirmRebateAmount());
        chargeMessage.setPricePp(chargeMessage.getAmountPp());
        chargeMessage.setCreateBy(annualRebateApproval.getCreateBy());
        chargeMessage.setCreateName(annualRebateApproval.getCreateName());
        chargeMessage.setCreateDate(new Date());
        chargeMessage.setUpdateBy(annualRebateApproval.getCreateBy());
        chargeMessage.setUpdateDate(chargeMessage.getCreateDate());
        chargeMessage.setUpdateName(annualRebateApproval.getCreateName());
        chargeMessage.setTenantId(annualRebateApproval.getTenantId());
        chargeMessage.setCompanyId(annualRebateApproval.getCompanyId());
        chargeMessage.setSettTemplate(FinanceSettTemplateEnum.PURCHASE_REBATE_TEMPLATE.getType());
        chargeMessage.setTaxRate(BigDecimal.ZERO);
        /**
         * version:V1.16.1: 新增【财务信息ID+结算主体类型】
         */
        chargeMessage.setAccountId(annualRebateApproval.getPayeeAccountId());
        chargeMessage.setBalanceType(NumberCst.TWO);
        this.doSendRecordToMq(annualRebateApproval.getRebateContractCode(), chargeMessage, this.financeTopic);
    }

    /**
     *
     * @param mcnIncomeFee
     * @param customerId
     */
    public void setBalance(CfChargeCommonMessage mcnIncomeFee, Long customerId) {
        List<Long> customerIds = new ArrayList<>(Collections.singletonList(customerId));
        Map<Long, BaseCustomer> customerMap = Objects.requireNonNull(baseInfoClient.getMcnCustomerByIds(customerIds)).getObj();
        mcnIncomeFee.setBalance(customerMap.get(customerId).getCustomerNameC());
    }
}
